<?php
	session_start();
	include_once("global.php");
	$uid = $_SESSION['name'];
	$smarty->assign("uid",$uid);
	//头部
	$sql_select_search = "select K_Name,K_KID from keywordinfo where K_search > '1' limit 0,4";
	$query_select_search = $mysqli->query($sql_select_search);
	while($result_select_search = $query_select_search->fetch_assoc()){
		$assign_search[] = array(	"num"	=>	$result_select_search['K_KID'],
									"name"	=>	$result_select_search['K_Name']);
	}
	$smarty->assign("assign_search",$assign_search);/*热门关键词*/
	
	$sql_select_skeyword = "select K_Name,K_KID from keywordinfo where K_search = '1'";
	$query_select_skeyword = $mysqli->query($sql_select_skeyword);
	while($result_select_skeyword = $query_select_skeyword->fetch_assoc()){
		$assign_skeyword[] = array(	"num"	=>	$result_select_skeyword['K_KID'],
									"name"	=>	$result_select_skeyword['K_Name']);
	}
	$smarty->assign("assign_skeyword",$assign_skeyword);/*搜索框内关键词*/
	
	$sql_select_articlecategory = "select * from categoryinfo limit 0,2";
	$query_select_articlecategory = $mysqli->query($sql_select_articlecategory);
	while($result_select_articlecategory = $query_select_articlecategory->fetch_assoc()){
		$assign_artcategory[] = array (	"id"	=>	$result_select_articlecategory['C_CID'],
										"name"	=>	$result_select_articlecategory['C_Categoryname']);
	}
	$smarty->assign("assign_artcategory",$assign_artcategory);/*导航条*/
	
	$sql_select_countapp = "select * from appinfo";
	$query_select_countapp = $mysqli->query($sql_select_countapp);
	$result_select_countapp = $query_select_countapp->num_rows;
	$assign_countapp = $result_select_countapp;
	$smarty->assign("assign_countapp",$assign_countapp);/*站内APP统计*/
	
	$random = rand(50,$assign_countapp);
	$assign_radnom = $random;
	$smarty->assign("assign_radnom",$assign_radnom);	
	
	//通栏推广位
	$sql_select_banner = "select * from pushinfo where P_Pushid >30";
	$query_select_banner = $mysqli->query($sql_select_banner);
	while($result_select_banner = $query_select_banner->fetch_assoc()){
		$banner_appid = $result_select_banner['P_Pushappid'];
		$banner_apppid = $result_select_banner['P_Pushpid'];
		
		$sql_select_bannerpic = "select * from pictureinfo where P_PID = '$banner_apppid'";
		$query_select_bannerpic = $mysqli->query($sql_select_bannerpic);
		$result_select_bannerpic = $query_select_bannerpic->fetch_assoc();
		
		$sql_select_bannerapp = "select * from appinfo where A_Apptrackid = '$banner_appid'";
		$query_select_bannerapp = $mysqli->query($sql_select_bannerapp);
		$result_select_bannerapp = $query_select_bannerapp->fetch_assoc();
		
		$assign_bannerapp[] = array(	"id"	=>	$banner_appid,
										"name"	=>	$result_select_bannerapp['A_Apptrackname'],
										"url"	=>	"images/".$result_select_bannerpic['P_Name'].".png",);
	}
	$smarty->assign("assign_bannerapp",$assign_bannerapp);/*顶部通栏，首页通栏1，首页通栏2*/
	
	//右侧
	$sql_select_rightapp = "select P_Pushappid,P_Pushpid from pushinfo where P_Pushid >'20' and P_Pushid < '23'";
	$query_select_rightapp = $mysqli->query($sql_select_rightapp);
	while($result_select_rightapp = $query_select_rightapp->fetch_assoc()){
		$app_pid = $result_select_rightapp['P_Pushpid'];
		$app_id = $result_select_rightapp['P_Pushappid'];

		$sql_select_rightapppid = "select P_Name from pictureinfo where P_PID = '$app_pid'";
		$query_select_rightapppid = $mysqli->query($sql_select_rightapppid);
		$result_select_rightapppid = $query_select_rightapppid->fetch_assoc();
		$apppic = $result_select_rightapppid['P_Name'];
		
		$sql_select_rightappname = "select A_Apptrackname from appinfo where A_Apptrackid = '$app_id'";
		$query_select_rightappname = $mysqli->query($sql_select_rightappname);
		$result_select_rightappname = $query_select_rightappname->fetch_assoc();
		
		$assign_rightapp[] = array(	"id"	=>	$app_id,
									"pic"	=>	$apppic,
									"name"	=>	$result_select_rightappname['A_Apptrackname']);
	}
	$smarty->assign("assign_rightapp",$assign_rightapp);/*右侧APP推广*/


	//APP排行榜
	$sql_app_rank = "select * from appinfo where A_Appprice = '免费' order by A_Appdate desc limit 0,10";
	$query_app_rank = $mysqli->query($sql_app_rank);
	while($result_app_rank = $query_app_rank->fetch_assoc()){
		$assign_apprank1[] = array(	"id"	=>	$result_app_rank['A_Apptrackid'],
									"name"	=>	$result_app_rank['A_Apptrackname'],
									"icon"	=>	$result_app_rank['A_Appicon'],
								);
	}
	for($m=0;$m<10;$m++){
		$assign_apprank[] = array(	"num"	=>	$m+1,
									"id"	=>	$assign_apprank1[$m][id],
									"name"	=>	$assign_apprank1[$m][name],
									"icon"	=>	$assign_apprank1[$m][icon],
								);
	}
	$smarty->assign("assign_apprank",$assign_apprank);/*免费排名*/
	
	//左侧
	$url = $_SERVER['REQUEST_URI'];
	$smarty->assign("url",$url);
	$url_arr = parse_url($url);
	parse_str($url_arr['query'],$url_arr);
	$url_path = $url['path'];
	$url_page = $url_arr[page];//页码
	$url_id = $url_arr[id];//越狱,IOS
	$url_pri = $url_arr[pri];//免费,付费
	$url_ca = $url_arr[ca];//图书，游戏，教育
	$url_kw = $url_arr[kw];//益智、射击、动作
	$url_pre = $url_arr[pre];
	$url_order = $url_arr[order];
	$assign_per = array($url_id,$url_pri,$url_ca,$url_kw,$url_page,$url_pre,$url_order);
	$smarty->assign("assign_per",$assign_per);
	

	if($url_order == '0'){
		$order_id = "order by A_Appdate desc";
	}else{
		$order_id = "order by A_Appcount desc";
	}	
	if($url_id == '2'){
		$urlid = " A_Class='".$url_id."'";
	}else{
		$urlid = " A_Class= '0'";
	}
	if($url_pri == '2'){
		$urlpri = " and A_Appprice != '免费'";
	}else if($url_pri == '1'){
		$urlpri = " and A_Appprice = '免费'";
	}else if($url_pri == ''){
		$urlpri = "";
	}
	if($url_kw && $url_pre){
		$urlkw = " and A_Appkid like '%".$url_pre."%' and A_Categoryid = '".$url_kw."'";
	}else if($url_kw && !$url_pre){
		$urlkw = " and A_Categoryid ='".$url_kw."'";
	}else if(!$url_kw && !$url_pre){
		$urlkw = '';
	}
	$condition = "where ".$urlid.$urlpri.$urlkw;
	$sql_select_appinfo = "select * from appinfo ".$condition." ".$order_id." ".$limit;

	$query_select_appinfo = $mysqli->query($sql_select_appinfo);
	$total = $query_select_appinfo->num_rows;
	$num = "24";
	$id = $url_id;
	$pri = $url_pri;
	$pre = $url_pre;
	$kw = $url_kw;
	$ca = $url_ca;
	$order = $url_order;
	$pagenum = ceil($total/$num);	
	$page = new Page($total,$num,$id,$pri,$ca,$kw,$pre,$order);
	$limit = $page->limit_start();

	if($url_id > 2 || $url_pri > 2 || $url_ca > 2 || $url_order >1){
		$smarty->display("header.html");
		$smarty->display("404.html");
		$smarty->display("footer.html");
	}else{	
		$assign_first = $page->appFirst();
		$smarty->assign("assign_first",$assign_first);
		$assign_pre = $page->appPre();
		$smarty->assign("assign_pre",$assign_pre);
		$assign_list = $page->appList();
		$smarty->assign("assign_list",$assign_list);
		$assign_next = $page->appNext();
		$smarty->assign("assign_next",$assign_next);
		$assign_last = $page->appLast();
		$smarty->assign("assign_last",$assign_last);
		
		$currentpage = $page->page();
		$sql_select_appinfo = "select * from appinfo ".$condition." ".$order_id." ".$limit;
		$query_select_appinfo = $mysqli->query($sql_select_appinfo);
		while($result_select_appinfo =$query_select_appinfo->fetch_assoc()){
			$kid = $result_select_appinfo['A_Appkid'];
			$kid_explode = explode(",",$kid);
			foreach($kid_explode as $key=>$value){
				$value = $kid_explode[$key];
				$sql_select_appkid = "select * from keywordinfo where K_KID = '$value'";
				$query_select_appkid = $mysqli->query($sql_select_appkid);
				$result_select_appkid = $query_select_appkid->fetch_assoc();
			}
			$assign_app[] = array(	"id"	=>	$result_select_appinfo['A_Appid'],
									"trackid"	=> $result_select_appinfo['A_Apptrackid'],
									"trackname"	=>	$result_select_appinfo['A_Apptrackname'],
									"icon"		=>	$result_select_appinfo['A_Appicon'],
									"kid"		=>	$result_select_appinfo['A_Appkid']);
		}
		$smarty->assign("assign_app",$assign_app);
		
		$sql_select_keyword = "select * from keywordinfo where K_Name like '%游戏%' and K_ID != 'NULL'";
		$query_select_keyword = $mysqli->query($sql_select_keyword);
		while($result_select_keyword = $query_select_keyword->fetch_assoc()){
			$assign_keyword[] = array(	"id"	=>	$result_select_keyword['K_KID'],
										"name"	=>	$result_select_keyword['K_Name'],);
		}
		$smarty->assign("assign_keyword",$assign_keyword);
	
	
		$sql_select_category = "select * from appcategoryinfo order by AC_Categoryid Asc";
		$query_select_category = $mysqli->query($sql_select_category);
		while($result_select_category = $query_select_category->fetch_assoc()){
			$assign_category[] = array(	"id"	=>	$result_select_category['AC_Categoryid'],
										"name"	=>	$result_select_category['AC_Categoryname'],);
		}
		$smarty->assign("assign_category",$assign_category);
		
		$sql_site_info = "select * from siteinfo";
		$query_site_info = $mysqli->query($sql_site_info);
		while($result_site_info = $query_site_info->fetch_assoc()){
			$assign_info[] = array(	"id"	=>	$result_site_info['S_SID'],
									"info"	=>	$result_site_info['S_Name'],
									);
		}
		$smarty->assign("assign_info",$assign_info);
		$smarty->display("app_list.html");
	}
?>